package com.ljd.gulimall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.ljd.gulimall.product.entity.AttrEntity;
import com.ljd.gulimall.product.service.AttrAttrgroupRelationService;
import com.ljd.gulimall.product.service.AttrService;
import com.ljd.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljd.common.utils.PageUtils;
import com.ljd.common.utils.Query;

import com.ljd.gulimall.product.dao.AttrGroupDao;
import com.ljd.gulimall.product.entity.AttrGroupEntity;
import com.ljd.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService relationService;
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)) {
            wrapper.and(ew -> {
                ew.eq("attr_group_id", key).or().like("attr_group_name",key);
            });
        }
        IPage<AttrGroupEntity> page;
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> relationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntityList = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        if(relationEntityList != null && relationEntityList.size() > 0) {
            List<Long> attrIds = relationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            Collection<AttrEntity> attrEntities = attrService.listByIds(attrIds);
            return (List<AttrEntity>) attrEntities;
        }
        return null;
    }

    @Override
    public void delRelation(List<AttrAttrgroupRelationEntity> attrGroups) {
        relationService.deleteBatch(attrGroups);
    }

    @Override
    public PageUtils noRelationAttr(Map<String, Object> params, Long attrgroupId) {

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)) {
            wrapper.and(ew -> {
                ew.eq("attr_id", key).or().like("attr_name",key);
            });
        }

        AttrGroupEntity attrGroup = this.baseMapper.selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrgroupId));
        if(attrGroup == null) {
            return new PageUtils(new Page<>());
        }

        Long catelogId = attrGroup.getCatelogId();

        // 查询当前分类关联所有属性分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 查询分组关联的所有属性
        List<Long> groupIds = attrGroupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        if(groupIds.size() > 0) {
            List<AttrAttrgroupRelationEntity> relationEntities = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", groupIds));
            List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            if(attrIds.size() > 0) {
                wrapper.notIn("attr_id", attrIds);
            }
        }

        wrapper.eq("catelog_id", catelogId).eq("attr_type", 1);
        IPage<AttrEntity> page;
        page = attrService.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void relation(List<AttrAttrgroupRelationEntity> attrGroups) {
        relationService.saveBatch(attrGroups);
    }

    @Override
    public List<AttrGroupWithAttrsVo> withattr(Long catelogId) {
        // 查询属性分组信息
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        return groupEntities.stream().map(e -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(e, vo);
            List<AttrEntity> attr = this.relationAttr(e.getAttrGroupId());
            vo.setAttrs(attr);
            return vo;
        }).collect(Collectors.toList());
    }

}